<?php

namespace app\common\lib;

class CheckDate{

    public static function check($type,$circulation_time){
        switch ($type) {
            case 'week':
                if (intval($circulation_time) <= 0 || intval($circulation_time) > 7) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                break;
            case 'month':
                if (intval($circulation_time) <= 0 || intval($circulation_time) > 31) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                break;
            case 'year':
                $yearArr = explode('-', $circulation_time);
                if (count($yearArr) < 2) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                self::checkDate($yearArr[0],$yearArr[1]);
                break;
            default:
                break;
        }
    }

    public function checkDate($month,$day){
        $month = intval($month);
        $day = intval($day);
        if (intval($month) <= 0 || intval($month) > 12 || !is_numeric($month)) {
            return Util::apiResponse([], '参数错误', 400);
        }
        switch($month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                if ($day <= 0 || $day > 31 || !is_numeric($month)) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                break;
            case 2:
                if ($day <= 0 || $day > 29 || !is_numeric($month)) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                if ($day <= 0 || $day > 30 || !is_numeric($month)) {
                    return Util::apiResponse([], '参数错误', 400);
                }
                break;
        }
    }
}